// import 'dart:html';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:search_people/page/home/main/views/dynamic_cell.dart';
import 'package:search_people/page/home/subs/person_detail/models/person_base_model/data.dart';
import 'package:search_people/page/home/subs/person_detail/models/person_dynamic_model.dart';
import 'package:search_people/page/home/subs/person_detail/models/person_history_model.dart';
import 'package:search_people/utils/component/list/section.dart';

import '../../../../utils/httputils/net_api.dart';
import 'index.dart';

class PersonDetailController extends GetxController
    with GetSingleTickerProviderStateMixin {
  PersonDetailController();

  final state = PersonDetailState();

  //获取基本信息
  void getPersonBaseInfo() async {
    var res = await Api.getPersonBaseInfo(state.leaderId!);
    var data = res.data;
    state.baseMd = PersonBaseModel.fromJson(data);
    update();
  }

  //获取任务历史
  void getPersonHistory() async {
    var resultData = await Api.getPersonHistory(state.leaderId!);
    List data = resultData.data;
    state.historyMds = data.map((e) => PersonHistoryModel.fromJson(e)).toList();

    update();
  }

  //获取人物政绩
  void getCharacterMap() async {
    var resultData = await Api.getCharacterMap(state.leaderId!);
    var data = resultData.data;
    List fellow = data['fellow'] ?? [];
    List colleague = data['colleague'] ?? [];
    List alumni = data['alumni'] ?? [];
    state.fellow = fellow.map((e) => PersonBaseModel.fromJson(e)).toList();
    state.colleague =
        colleague.map((e) => PersonBaseModel.fromJson(e)).toList();
    state.alumni = alumni.map((e) => PersonBaseModel.fromJson(e)).toList();
    state.pTitles = [
      '按老乡(${fellow.length})',
      '按校友(${alumni.length})',
      '按同事(${colleague.length})'
    ];
    update();
  }

  //获取工作动态
  void getPersonDynamic() async {
    var resultData = await Api.getPersonDynamics(state.leaderId!);
    List data = resultData.data['records'];
    state.dynamics = data.map((e) => PersonDynamicModel.fromJson(e)).toList();

    state.list = [
      SectionModel(
          index: 0,
          itemBuilder: (ctx, index) {
            var md = state.dynamics![index];
            return DynamicCell(
              md: md,
            );
          },
          items: state.dynamics ?? [])
    ];
    update();
  }

  void changeLeaderId(String? id) {
    state.leaderId = id;
    if (state.leaderId == null) {
      return;
    }
    state.sc.jumpTo(0);
    getPersonBaseInfo();
    getPersonHistory();
    getCharacterMap();
    getPersonDynamic();
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();

    state.sc = ScrollController();
    state.leaderId = Get.arguments;
    // if(GetPlatform.isWeb) {
    //   var url = window.location.href;

    //   if(url.contains('leaderId')){
    //     state.leaderId = url.substring(url.lastIndexOf('/')+1);
    //   }
    // }

    state.pTc = TabController(length: 3, vsync: this)
      ..addListener(() {
        if (state.pTc.index != state.pTc.previousIndex) {
          update();
        }
      });
    state.pTitles = ['按老乡', '按校友', '按同事'];
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    changeLeaderId(state.leaderId);
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }
}
